<template>
  <div class="search-result">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.art_id" :title="item.title" />
    </van-list>
  </div>
</template>

<script>
import { getResult } from "@/api/search";
export default {
  name: "SearchResult",
  components: {},
  props: {
    q: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async onLoad() {
      // 异步更新数据
      try {
        let { data: res } = await getResult({
          page: this.page,
          per_page: 10,
          q: this.q
        });
        // 追加数据
        this.list.push(...res.data.results);
        // 结束loading状态
        this.loading = false;
        // 判断是否结束
        if (res.data.results.length < 10) {
          this.finished = true;
        } else {
          this.page++;
        }
      } catch (error) {
        this.$toast.fail("获取结果列表失败");
      }
    }
  }
};
</script>

<style scoped lang="less"></style>
